package com.sskj.gocoin.v.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.airsaid.pickerviewlibrary.TimePickerView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.sskj.gocoin.common.ClassicsHeader;
import com.sskj.gocoin.R;
import com.sskj.gocoin.common.App;
import com.sskj.gocoin.common.adapter.BaseAdapter;
import com.sskj.gocoin.common.adapter.ViewHolder;
import com.sskj.gocoin.common.decoration.DividerLineItemDecoration;
import com.sskj.gocoin.m.bean.TradeDealBean;
import com.sskj.gocoin.p.fragment.TradeDealFragmentPst;
import com.sskj.gocoin.utils.ClickUtil;
import com.sskj.gocoin.utils.DateUtil;
import com.sskj.gocoin.utils.ToastUtil;
import com.sskj.gocoin.v.activity.TradeActivity;
import com.sskj.gocoin.v.base.BaseViewPagerFragment;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;


/**
 * 杠杆交易--成交订单
 * Created by Lee on 2018/2/28 0028.
 */

public class TradeDealFragment extends BaseViewPagerFragment<TradeActivity, TradeDealFragmentPst> {


    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.ll_no_data)
    LinearLayout llNoData;
    Unbinder unbinder;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.riqi_tv1)
    TextView riqiTv1;
    @BindView(R.id.riqi_tv2)
    TextView startTv;
    @BindView(R.id.qishi_riqi)
    RelativeLayout qishiRiqi;
    @BindView(R.id.jiezhi_riqi_tv1)
    TextView jiezhiRiqiTv1;
    @BindView(R.id.jiezhi_riqi_tv2)
    TextView endTv;
    @BindView(R.id.jiezhi_riqi)
    RelativeLayout jiezhiRiqi;
    Unbinder unbinder1;
    @BindView(R.id.date_show)
    LinearLayout dateShow;
    private BaseAdapter<TradeDealBean> mAdapter;
    private String startTime;
    private String endTime;


    @Override
    public int getLayoutId() {
        return R.layout.fragment_recyclerview;
    }

    @Override
    public TradeDealFragmentPst getPresenter() {
        return new TradeDealFragmentPst();
    }

    @Override
    public void updateData() {

    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        dateShow.setVisibility(View.VISIBLE);
        if (mAdapter == null) {
            recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
            recyclerView.addItemDecoration(new DividerLineItemDecoration(getActivity()).setFirstDraw(false));
            mAdapter = new BaseAdapter<TradeDealBean>(R.layout.recy_item_trade_deal, null, recyclerView) {
                @Override
                public void bind(ViewHolder holder, TradeDealBean item) {

                    holder.setText(R.id.tv_lx, item.getOtype() == 1 ? App.INSTANCE.getString(R.string.strNewTradePositionFragment209) : App.INSTANCE.getString(R.string.strNewTradePositionFragment210));
                    holder.setTextColor(R.id.tv_lx, getResources().getColor(1 == item.getOtype() ? R.color.market_up : R.color.market_down));
                    holder.setText(R.id.tv_jc_date, App.INSTANCE.getString(R.string.xmlrecy_item_trade_deal51) + "  " + item.getAddtime());  // 建仓时间
                    holder.setText(R.id.tv_pc_date, App.INSTANCE.getString(R.string.xmlrecy_item_trade_deal63) + "  " + item.getSelltime());  // 平仓时间
                    holder.setText(R.id.tv_bz, item.getPname());
                    holder.setText(R.id.tv_time, item.getSelltime());
                    holder.setText(R.id.tv_ccj, App.INSTANCE.getString(R.string.xmlrecy_item_trade_deal80) + "  " + item.getBuyprice());  // 持仓价
                    holder.setText(R.id.tv_pcj, App.INSTANCE.getString(R.string.xmlrecy_item_trade_deal92) + "  " + item.getSellprice());  // 平仓价
                    holder.setText(R.id.tv_cjl, App.INSTANCE.getString(R.string.xmlrecy_item_trade_deal109) + "  " + item.getBuynum());  // 成交量
                    holder.setText(R.id.tv_zje, App.INSTANCE.getString(R.string.trade_sec) + "  " + item.getTotalprice());  // 保证金
                    holder.setText(R.id.tv_sxf, App.INSTANCE.getString(R.string.xmlrecy_item_trade_deal138) + "  " + item.getSxfee());  // 手续费
                    holder.setText(R.id.tv_dayfee, App.INSTANCE.getString(R.string.xmlrecy_item_trade_deal150) + "  " + item.getDayfee());  // 利息
                    holder.setText(R.id.tv_stopwin, App.INSTANCE.getString(R.string.xmllayout_head_view217) + "  " + item.getPoit_win());  // 止盈价格
                    holder.setText(R.id.tv_stoploss, App.INSTANCE.getString(R.string.xmllayout_head_view236) + "  " + item.getPoit_loss());  // 止损价格
                    holder.setText(R.id.tv_pc_type, App.INSTANCE.getString(R.string.xmlrecy_item_trade_deal209) + "  " + (item.getPc_type() == 1 ? App.INSTANCE.getString(R.string.strTradeDealFragment202)
                            : item.getPc_type() == 2 ? App.INSTANCE.getString(R.string.strTradeDealFragment203)
                            : item.getPc_type() == 3 ? App.INSTANCE.getString(R.string.strTradeDealFragment204)
                            : item.getPc_type() == 4 ? App.INSTANCE.getString(R.string.strTradeDealFragment205) : ""));  // 平仓类型
                    holder.setText(R.id.tv_fdyk, App.INSTANCE.getString(R.string.xmlrecy_item_trade_deal197) + "  " + item.getProfit());  // 浮动盈亏
                    // 浮动盈亏设置红色绿色（红涨绿跌）
                    if (item.getProfit().contains("-")) {
                        holder.setTextColor(R.id.fdykText, getResources().getColor(R.color.market_down));
                        holder.setTextColor(R.id.tv_fdyk, getResources().getColor(R.color.market_down));
                    } else {
                        holder.setTextColor(R.id.tv_fdyk, getResources().getColor(R.color.market_up));
                        holder.setTextColor(R.id.fdykText, getResources().getColor(R.color.market_up));
                    }
                }
            };
            mAdapter.setEmtyView();
        }
        startTime();
        ClickUtil.click(qishiRiqi, () -> {  // 起始日期
            startTime(true, startTv);
        });
        ClickUtil.click(jiezhiRiqi, () -> {  // 截止日期
            startTime(false, endTv);
        });
        ClassicsHeader classicsHeader = new ClassicsHeader(getActivity());
        classicsHeader.setBackgroundColor(getResources().getColor(R.color.background));
        refreshLayout.setRefreshHeader(classicsHeader);
        refreshLayout.setOnRefreshListener(refreshLayout -> {
            refreshLayout.finishRefresh();
            mPresenter.loadData(startTime + " 00:00:00", endTime + " 23:59:59");
        });
        refreshLayout.autoRefresh();


    }


    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            if (!TextUtils.isEmpty(startTime) && !TextUtils.isEmpty(endTime)) {
                mPresenter.loadData(startTime + " 00:00:00", endTime + " 23:59:59");
            } else {
                mPresenter.loadData();
            }
        }
    }

    public void updateData(List<TradeDealBean> data) {
        mAdapter.setNewData(data);
    }

    public void startTime(boolean flag, TextView view) {  // true起始时间
        TimePickerView mTimePickerView = new TimePickerView(getActivity(), TimePickerView.Type.YEAR_MONTH_DAY);
        mTimePickerView.setOnTimeSelectListener(new TimePickerView.OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date) {
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
                String format1 = format.format(date);
                view.setText(format1);
                if (flag) {
                    startTime = format1;
                } else {
                    endTime = format1;
                    if (!DateUtil.isTrueDateTwo(startTime, endTime)) {
                        ToastUtil.showShort(App.INSTANCE.getString(R.string.strTradeDealFragment213));
                        return;
                    }
                    mPresenter.loadData(startTime + " 00:00:00", endTime + " 23:59:59");
                }
            }
        });
        mTimePickerView.show();
    }

    //默认时间加载
    public void startTime() {
        endTime = DateUtil.getCurrentDate(DateUtil.PATTERN_E);
        Calendar ca = Calendar.getInstance();
        ca.add(Calendar.MONTH, -1);
        startTime = DateUtil.getDateFormat(ca.getTime(), DateUtil.PATTERN_E);
        startTv.setText(startTime);
        endTv.setText(endTime);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder1 = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder1.unbind();
    }
}
